home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / AIFF DSP v22 / plugin_src / card.c < prev    next >
Text File  |  1995-01-30  |  2KB  |  110 lines

  1. /*
  2. FILE:    card.c
  3. PROJECT: Ford grant DSP
  4. AUTHOR:  Ben Denckla
  5. COMMENT: Analyzes magnetic card waveform.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <assert.h>
  11. #include <string.h>
  12.  
  13. #include "aiff.h"
  14. #include "card.h"
  15.  
  16. char *histogram( pkpt *p, short pts ) {
  17. #define BARMAX 60
  18. #define BINS 251
  19.  
  20.    char bar[BARMAX];
  21.    static char d[200];
  22.    char errstr[100];
  23.    short i, barlen, hist[BINS] = {0}, hmax = 0, d1, d2 = 0, rat;
  24.    
  25.    for (i=1; i<pts; i++) {
  26.       d1 = p[i].pos - p[i-1].pos;
  27.       if ( d2==0 ) rat = 100;
  28.       else rat = (double) 100 * d1/d2;
  29.       if ( rat < BINS ) hist[rat]++;
  30.       else
  31.       {
  32.          sprintf( errstr, "%hd out o\f bin range", rat );
  33.          err( errstr );
  34.       }
  35.       d2 = d1;
  36.    }
  37.    memset( bar, '*', BARMAX );
  38.    for (i=0; i<BINS; i++) if (hist[i] > hmax) hmax = hist[i];
  39.  
  40.    for (i=0; i<BINS; i++) {
  41.       barlen = ((BARMAX+1) * hist[i])/(hmax+1);
  42.       printf( "bin %4d: %5d %.*s\n", i, hist[i], barlen, bar );
  43.    }
  44.    return d;
  45. }
  46.  
  47. void printout( pkpt *p, short pts) {
  48.    short i;
  49.    
  50.    for (i=0; i<pts; i++) printf( "%ld\n", p[i].pos - p->pos );
  51. }
  52.  
  53. void readinfil( FILE *peakfil, pkpt **p, short *pts ) {
  54.    long int filsiz;
  55.  
  56.    assert( !fseek( peakfil, 0, SEEK_END ) );
  57.    assert( (filsiz = ftell( peakfil )) != -1L );
  58.  
  59.    assert( *p = malloc( filsiz ) );
  60.    *pts = filsiz / sizeof(pkpt);
  61.    
  62.    assert( !fseek( peakfil, 0, SEEK_SET ) );
  63.    assert( fread( *p, filsiz, 1, peakfil ) );
  64.    assert( !fclose( peakfil ) );
  65. }
  66.  
  67.  
  68. char map( char *s ) {
  69.    short i; char tmp = 0, scratch[2];
  70.  
  71.    for (i=0; i<4; i++) if (s[3-i]) tmp |= 1<<i;
  72.    sprintf( scratch, "%x", tmp );
  73.    return *scratch;
  74. }
  75.  
  76. void process_bits( char *d, short quads ) {
  77.    char ld[200], *td, *tld, out[50], *tout;
  78.    short i;
  79.    
  80.    td = &d[quads-1];
  81.    tld = ld;
  82.    
  83.    for (i=0; i<quads; i++) {
  84.       putchar( (*td) ? '1' : '.' );
  85.       *tld++ = *td--;
  86.    }
  87.    putchar( '\n' );
  88.    
  89.    for (i=0; !ld[i]; i++); printf( "skipped %d zeroes\n", i );
  90.    
  91.    for (tout=out; i<quads; i+=5) *tout++ = map(&ld[i]);
  92.    *tout = 0;
  93.    printf( "%s\n", out );
  94. }
  95.  
  96. void post_process ( FILE *peakfil ) {
  97.    pkpt *p;
  98.    char *d;
  99.    short pts;
  100.    
  101.    readinfil( peakfil, &p, &pts );
  102.    
  103.    /*printout( p, pts ); */
  104.    d = histogram( p, pts );
  105.    
  106.    /*process_bits( d, quads ); */
  107.  
  108.    free( p );
  109. }
  110.